//
//  HDApplyIndemnityArchivesCell.swift
//  HuaDaoPeifu
//
//  Created by 刘杰 on 2018/7/24.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import UIKit
// import HXPhotoPicker

class HDApplyIndemnityArchivesCell: UITableViewCell {
    
    @IBOutlet weak var typeNameLB: UILabel!
    @IBOutlet weak var alreadyIncludeCountLB: UILabel!
    
    @IBOutlet weak var star: UILabel!
    @IBOutlet weak var necessaryLB: UILabel!
    @IBOutlet weak var demoButton: UIButton!
    @IBOutlet weak var demoLB: UILabel!
    @IBOutlet weak var accessoryLB: UIImageView!
    
    var showDemoButton: Bool? {
        didSet{
            demoLB.isHidden = !showDemoButton!
            demoButton.isHidden = !showDemoButton!
            accessoryLB.isHidden = !showDemoButton!
        }
    }
    
    var photoViewChangeHeightBlock:((_ cell:HDApplyIndemnityArchivesCell)->Void)?
    
    var showDemoImages:((_ cell:HDApplyIndemnityArchivesCell)->())?
    
    var isNecessary : Bool? {
        didSet{
            necessaryLB.isHidden = !isNecessary!
            star.isHidden = !isNecessary!
        }
    }
    
    var key : String?{
        didSet{
            let title = HDArchiveTypeHelper.sharedInstance.typeTitle(TypeId: key!)
            typeNameLB.text = title
            self.title = title
        }
    }
    
    var title : String?
    
    var model = HDApplyIndemnityArchivesCellModel()
    let manager = HXPhotoManager(type: HXPhotoManagerSelectedTypePhoto)
    fileprivate let toolManager = HXDatePhotoToolManager.init()
    fileprivate var photoView:HXPhotoView?
    
    fileprivate func setupUI() {
        self.selectionStyle = .none
        photoView = HXPhotoView(frame: CGRect(x: CGFloat(AppConstant.screenWidth-300.0) * CGFloat(0.5), y: 50, width: 300, height: 0), manager: manager)
        photoView?.manager.configuration.navBarBackgroudColor = UIColor.white
        photoView?.manager.configuration.photoMaxNum = 1000
        photoView?.manager.configuration.navBarBackgroudColor = UIColor.white
        photoView?.hideDeleteButton = true
        photoView?.previewShowDeleteButton = false
        photoView?.manager.configuration.photoCanEdit = false
        photoView?.delegate = self
        self.contentView.addSubview(photoView!)
        
        /// 单元格默认的高度
        model.cellHeight = self.bounds.height
        model.photoViewHeight = self.bounds.height
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    func refreshPhotoView() {
        photoView?.refreshView()
    }
    
    @IBAction func showDemoImageAction(_ sender: UIButton) {
        
//        guard let urls = model?.demoUrls else {
//            return
//        }
//
//        var urlStrings = [String]()
//        //模型数据数组
//        for url in urls {
//            urlStrings.append(kBaseApiv2URL+"/picc/"+url.description)
//        }
        if let showDemoImages = showDemoImages {
            showDemoImages(self)
        }
    }
}

extension HDApplyIndemnityArchivesCell:HXPhotoViewDelegate{
    
    func photoView(_ photoView: HXPhotoView!, updateFrame frame: CGRect) {
        if frame.size.height == (self.model.photoViewHeight-60) {
            return
        }
        self.model.photoViewHeight = frame.size.height + 60
        if (self.photoViewChangeHeightBlock != nil) {
            self.photoViewChangeHeightBlock!(self)
        }
    }
    
    func photoView(_ photoView: HXPhotoView!, changeComplete allList: [HXPhotoModel]!, photos: [HXPhotoModel]!, videos: [HXPhotoModel]!, original isOriginal: Bool) {
      
        alreadyIncludeCountLB.text = "已传\(photos.count)张"
        
        var localPhotos = [HXPhotoModel]()
        for photo in photos {
            if photo.networkPhotoUrl == nil {
                localPhotos.append(photo)
            }
        }
        
        toolManager.getSelectedImageList(photos, success: {[unowned self] (list:[UIImage]?) in
            
            if let list = list {
                self.model.allPhotos = list
            }
        }) {
        }
        
        toolManager.getSelectedImageList(localPhotos, success: {[unowned self] (list:[UIImage]?) in
            
            if let list = list {
                self.model.newPhotos = list
            }
        }) {
        }
    }
}

class HDApplyIndemnityArchivesCellModel: NSObject {
    var cellHeight:CGFloat = 0
    var photoViewHeight:CGFloat = 0
    var newPhotos = [UIImage]()
    var allPhotos = [UIImage]()
    var demoUrls = [Substring]()
}
